.TH std::ostreambuf_iterator::ostreambuf_iterator 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ostreambuf_iterator::ostreambuf_iterator \- std::ostreambuf_iterator::ostreambuf_iterator

.SH Synopsis
   ostreambuf_iterator( streambuf_type* buffer )            \fI(until C++11)\fP
   throw();
   ostreambuf_iterator( streambuf_type* buffer )            \fI(since C++11)\fP
   noexcept;                                        \fB(1)\fP
   ostreambuf_iterator( ostream_type& stream )                            \fI(until C++11)\fP
   throw();                                             \fB(2)\fP
   ostreambuf_iterator( ostream_type& stream )                            \fI(since C++11)\fP
   noexcept;

   1) Constructs the iterator with the private streambuf_type* member set to buffer and
   the failed() flag set to false. The behavior is undefined if buffer is a null
   pointer.
   2) Same as ostreambuf_iterator(stream.rdbuf()).

.SH Parameters

   stream - the output stream whose rdbuf() will be accessed by this iterator
   buffer - the output stream buffer to be accessed by this iterator

.SH Example


// Run this code

 #include <fstream>
 #include <iostream>
 #include <iterator>

 int main()
 {
     const char* file = "test.txt";
     {
         std::basic_filebuf<char> f;
         f.open(file, std::ios::out);
         std::ostreambuf_iterator<char> out1(&f);
         *out1 = 'a'; // writes to file via iterator
     }

     // read back from the file
     char a;
     std::cout << ((std::ifstream{file} >> a), a) << std::endl;

     std::ostreambuf_iterator<wchar_t> out2{std::wcout};
     *out2 = L'b';
 }

.SH Output:

 a
 b

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to           Behavior as published            Correct behavior
   LWG 112 C++98      the requirement "the argument cannot      applies to overload
                      be null" was applied to overload \fB(2)\fP      \fB(1)\fP instead
   P2325R3 C++20      default constructor was provided as C++20 removed along with
                      iterators must be default_initializable   the requirement
